{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "90ec24de",
   "metadata": {},
   "outputs": [],
   "source": [
    "import findspark\n",
    "import requests\n",
    "import sys\n",
    "import jieba\n",
    "from bs4 import BeautifulSoup\n",
    "from pyspark import SparkContext, SparkConf\n",
    "findspark.init()\n",
    "conf = SparkConf()\n",
    "sc = SparkContext(conf=conf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cab5fd62",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检测网站中是否包含特定的关键词，如果包含统计命中次数\n",
    "count = sc.accumulator(0)\n",
    "\n",
    "#构建敏感词字典\n",
    "lines = open(\"C:/Users/Administrator/Desktop/demo/words.txt\",\"r\",encoding=\"utf-8\").read().split(\"\\n\")\n",
    "\n",
    "#敏感词词典用于统计\n",
    "WordsMap = {w:1 for w in lines}\n",
    "\n",
    "#加载URL列表\n",
    "urlRdd = sc.textFile(\"file:///C:/Users/Administrator/Desktop/demo/urls.txt\")\n",
    "\n",
    "headers = {\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a69f6b75",
   "metadata": {},
   "outputs": [],
   "source": [
    "#敏感词提取\n",
    "def extractFunc(url):\n",
    "\tglobal WordsMap\n",
    "\tres = requests.get(url, headers=headers)\n",
    "\t#转换编码\n",
    "\tres.encoding = 'UTF-8'\n",
    "\t#提取标签内部文字\n",
    "\tbs = BeautifulSoup(res.text, \"html.parser\")\n",
    "\tkeyWords = jieba.cut_for_search(bs.get_text())\n",
    "\n",
    "\tret = [ v for v in list(keyWords) if WordsMap.get(v,None)!=None]\n",
    "\tcount.add(len(ret))\n",
    "\n",
    "\treturn ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "23b3e8f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('学校', 92), ('项目', 84), ('科研', 64), ('学院', 63), ('洛阳', 59), ('教学', 53), ('通知', 48), ('河南', 45), ('新闻', 27), ('活动', 16)]\n",
      "742\n"
     ]
    }
   ],
   "source": [
    "result = urlRdd \\\n",
    "    .map(extractFunc) \\\n",
    "    .flatMap(lambda x:x) \\\n",
    "    .map(lambda x:(x,1)) \\\n",
    "    .reduceByKey(lambda x,y: x+y) \\\n",
    "    .sortBy(lambda x:-x[1])\n",
    "\n",
    "print(result.take(10))\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b30c2ff8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "e42634819b8c191a5d07eaf23810ff32516dd8d3875f28ec3e488928fbd3c187"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
